#!/bin/bash
# CMDB 服务状态查看脚本

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'

# 获取脚本所在目录的绝对路径
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

LOG_DIR="$SCRIPT_DIR/logs"
BACKEND_PID_FILE="$LOG_DIR/backend.pid"
FRONTEND_PID_FILE="$LOG_DIR/frontend.pid"

echo -e "${CYAN}=========================================="
echo "   CMDB - 服务状态"
echo -e "==========================================${NC}"
echo ""

# 检查后端状态
echo -e "${CYAN}🔧 后端服务 (Django + Channels):${NC}"
if [ -f "$BACKEND_PID_FILE" ]; then
    BACKEND_PID=$(cat "$BACKEND_PID_FILE")
    if kill -0 $BACKEND_PID 2>/dev/null; then
        echo -e "  状态: ${GREEN}✓ 运行中${NC}"
        echo -e "  PID: ${YELLOW}$BACKEND_PID${NC}"
        echo -e "  端口: ${YELLOW}8000${NC}"
        echo -e "  访问: ${CYAN}http://localhost:8000${NC}"
        
        # 检查端口
        if netstat -tuln 2>/dev/null | grep -q ":8000 " || ss -tuln 2>/dev/null | grep -q ":8000 "; then
            echo -e "  监听: ${GREEN}✓ 端口8000已监听${NC}"
        else
            echo -e "  监听: ${RED}✗ 端口8000未监听${NC}"
        fi
        
        # 显示进程信息
        UPTIME=$(ps -o etime= -p $BACKEND_PID 2>/dev/null | tr -d ' ')
        if [ -n "$UPTIME" ]; then
            echo -e "  运行时间: ${YELLOW}$UPTIME${NC}"
        fi
    else
        echo -e "  状态: ${RED}✗ 已停止${NC} (PID文件存在但进程不存在)"
        rm -f "$BACKEND_PID_FILE"
    fi
else
    echo -e "  状态: ${RED}✗ 未运行${NC}"
fi

echo ""

# 检查前端状态
echo -e "${CYAN}🎨 前端服务 (Vue 3 + Vite):${NC}"
if [ -f "$FRONTEND_PID_FILE" ]; then
    FRONTEND_PID=$(cat "$FRONTEND_PID_FILE")
    if kill -0 $FRONTEND_PID 2>/dev/null; then
        echo -e "  状态: ${GREEN}✓ 运行中${NC}"
        echo -e "  PID: ${YELLOW}$FRONTEND_PID${NC}"
        echo -e "  端口: ${YELLOW}8080${NC}"
        echo -e "  访问: ${CYAN}http://localhost:8080${NC}"
        
        # 检查端口
        if netstat -tuln 2>/dev/null | grep -q ":8080 " || ss -tuln 2>/dev/null | grep -q ":8080 "; then
            echo -e "  监听: ${GREEN}✓ 端口8080已监听${NC}"
        else
            echo -e "  监听: ${RED}✗ 端口8080未监听${NC}"
        fi
        
        # 显示进程信息
        UPTIME=$(ps -o etime= -p $FRONTEND_PID 2>/dev/null | tr -d ' ')
        if [ -n "$UPTIME" ]; then
            echo -e "  运行时间: ${YELLOW}$UPTIME${NC}"
        fi
    else
        echo -e "  状态: ${RED}✗ 已停止${NC} (PID文件存在但进程不存在)"
        rm -f "$FRONTEND_PID_FILE"
    fi
else
    echo -e "  状态: ${RED}✗ 未运行${NC}"
fi

echo ""
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"

# 检查端口占用情况
echo ""
echo -e "${CYAN}📊 端口占用情况:${NC}"
if command -v netstat &> /dev/null; then
    netstat -tuln 2>/dev/null | grep -E ":(8000|8080) " | head -5 || echo -e "  ${YELLOW}端口8000和8080未被占用${NC}"
elif command -v ss &> /dev/null; then
    ss -tuln 2>/dev/null | grep -E ":(8000|8080) " | head -5 || echo -e "  ${YELLOW}端口8000和8080未被占用${NC}"
else
    echo -e "  ${YELLOW}无法检测端口占用 (需要netstat或ss命令)${NC}"
fi

# 数据库状态
echo ""
echo -e "${CYAN}💾 数据库状态:${NC}"
if [ -f "backend/db.sqlite3" ]; then
    DB_SIZE=$(du -h backend/db.sqlite3 | cut -f1)
    echo -e "  文件: ${GREEN}✓ 存在${NC}"
    echo -e "  大小: ${YELLOW}$DB_SIZE${NC}"
    echo -e "  路径: ${CYAN}backend/db.sqlite3${NC}"
else
    echo -e "  文件: ${RED}✗ 不存在${NC}"
    echo -e "  提示: ${YELLOW}运行 './deploy.sh' 初始化数据库${NC}"
fi

echo ""
echo -e "${CYAN}=========================================="
echo "   管理命令"
echo -e "==========================================${NC}"
echo -e "  ${GREEN}./deploy.sh${NC}  - 启动所有服务"
echo -e "  ${GREEN}./stop.sh${NC}    - 停止所有服务"
echo -e "  ${GREEN}./logs.sh${NC}    - 查看服务日志"
echo -e "  ${GREEN}./restart.sh${NC} - 重启所有服务"
echo ""
